package Client.Dropbox.SystemCommands
import java.nio.file.Path
import java.io.File
import java.io.FileInputStream
import java.util.zip.Adler32
import java.util.zip.CheckedInputStream

/**
 * Object that converts a file into an array of bytes, and calculates
 * its CheckSum
 */
object SumGenerator {
  
  /**
   * Calculate the CheckSum of a file
   * @param file the file path
   * @returns the CheckSum
   */
  def createCheckSum (file:Path):Long={
    val fis=new FileInputStream(new File(file.toString))
    val checksum=new Adler32
    val cis=new CheckedInputStream(fis, checksum)
    val array=new Array[Byte](128)
    while ((cis.read(array)) >= 0) {}
    cis.close
    fis.close
    return checksum.getValue
  }
  
  /**
   * Convert a file into an array of bytes
   * @param path the file path
   * @returns the array of bytes
   */
  def toBytes (path:Path):Array[Byte]={
    val file=new File(path.toString)
    val fis=new FileInputStream(file)
    var bytes=new Array[Byte](file.length.asInstanceOf[Int])
    fis.read(bytes)
    fis.close
    return bytes
  }
}
